package com.yeqian.controller;

import cn.hutool.core.date.DateTime;
import com.yeqian.entity.PageResult;
import com.yeqian.entity.QueryPageBean;
import com.yeqian.entity.Result;
import com.yeqian.pojo.Comment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author : yeqian
 * @Date : 2021/1/6 22:27
 * 评论控制器
 */
@RestController
@RequestMapping("/comment")
@Api("文章控制器")
public class CommentController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(CommentController.class);

    /**
     * 柱状图
     *
     * @param
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/16 22:50
     */
    @PostMapping("/getHistogramReport")
    @ApiOperation("柱状图")
    public Result getHistogramReport() {
        /*
        格式：两个list集合
        * [820, 932, 901, 934, 1290, 1330, 1320]
        * ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        * */
  /*      //查询各标签占比
        Map<String,Object> list = new HashMap<>();
        List<Object> slist1 = new ArrayList<>();
        List<Object> slist2 = new ArrayList<>();
        slist1.add(82);
        slist1.add(83);
        slist1.add(85);
        slist1.add(72);

        slist2.add("2020/1/1");
        slist2.add("2020/1/2");
        slist2.add("2020/1/3");
        slist2.add("2020/1/4");
        list.put("number",slist1);
        list.put("months",slist2);*/

        Map<String, Object> map = new HashMap<>();
        Calendar instance = Calendar.getInstance();//获得日历对象
        //计算过去一年的12个月
        instance.add(Calendar.MONTH, -12);
        List<String> monthsList = new ArrayList<>();
        List<Integer> commentCountList = new ArrayList<>();
        Date month = null;
        String format = null;
        for (int i = 0; i < 12; i++) {
            instance.add(Calendar.MONTH, 1);
            month = instance.getTime();
            format = new SimpleDateFormat("yyyy.MM").format(month);
            monthsList.add(format);

            Integer commentCount = commentService.getCommentCountByMonth(month);
            commentCountList.add(commentCount);
        }

        map.put("months", monthsList);
        map.put("commentCount", commentCountList);

        return new Result(true, "柱状图", map);
    }


    /**
     * 查看所有评论
     *
     * @param author
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/10 10:38
     */
    @GetMapping("/getCommentCount/{id}")
    @ApiOperation("查询所有评论")
    public Result getCommentCount(@PathVariable("id") Integer author) {
        Integer commentCount = commentService.getCommentCount(author);
        return new Result(true, "查询所有评论成功", commentCount);
    }

    /**
     * 查看未读评论
     *
     * @param author
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/10 10:35
     */
    @GetMapping("/getUnread/{id}")
    @ApiOperation("查询未读评论")
    public Result getUnreadByauthor(@PathVariable("id") Integer author) {
        Integer unreadCount = commentService.getUnreadByauthor(author);
        return new Result(true, "查询未读评论成功", unreadCount);
    }

    /**
     * 展示文章对应的评论
     *
     * @param id
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/8 17:15
     */
    @GetMapping("/getComment/{id}")
    @ApiOperation("展示文章对应的评论")
    public Result getCommentByArticleId(@PathVariable("id") Integer id) {
        List<Comment> commentList = commentService.getCommentByArticleId(id);
        return new Result(true, "展示文章对应的评论查询成功", commentList);
    }

    /**
     * 回复邮件
     *
     * @param
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/7 22:57
     */
    @PostMapping("/sendEmail")
    @ApiOperation("回复邮件")
    public Result sendEmail(@RequestBody Comment comment) {
        String text = comment.getContent();
        // String text = "<p style='color:#0BA4E0'>"+comment.getContent()+ "</p>的评论";
        String title = "作者回复了你在《" + comment.getArticleName() + "》的评论";
        String to = comment.getEmail();
        boolean flag = mailUtils.sendSimpleMail(text, title, to); //回复邮件
        return new Result(true, "回复邮件成功", flag);
    }

    /**
     * 查询评论
     *
     * @param id
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/7 22:53
     */
    @GetMapping("/{id}")
    @ApiOperation("查询评论")
    public Result getComment(@PathVariable Integer id) {
        Comment comment = commentService.getComment(id);
        return new Result(true, "查询成功", comment);
    }

    /**
     * 分页查询评论
     *
     * @param queryPageBean
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/7 22:52
     */
    @PostMapping("/search")
    @ApiOperation("分页查询")
    public Result listPageQuery(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = commentService.listPageQuery(queryPageBean);
        return new Result(true, "分页查询成功", pageResult);
    }


    /**
     * 添加评论
     *
     * @param comment
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/7 22:12
     */
    @PostMapping
    @ApiOperation("添加")
    public Result saveComment(@RequestBody Comment comment, HttpServletRequest request) {
        comment.setTime(new DateTime());
        String remoteAddr = request.getRemoteAddr();
        System.out.println(request.getRemoteAddr());
        comment.setIp(remoteAddr);
        commentService.saveComment(comment);
        return new Result(true, "添加成功");
    }
}
